home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / system / srouted-.000 / srouted- / srouted-0.1pl1 / kernel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-20  |  4.2 KB  |  170 lines

  1. /* kernel.c -- interface to kernel structures/ioctls */
  2.  
  3. /*
  4.  *  srouted -- silent routing daemon
  5.  *  Copyright (C) 1995 Kevin Buhr
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with this program; if not, write to the Free Software
  19.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] = "$Id: kernel.c,v 1.4 1995/02/20 17:44:16 buhr Exp $";
  24. #endif /* not lint */
  25.  
  26. #include "defs.h"
  27. #include "table.h"
  28. #include "kernel.h"
  29.  
  30. #include <unistd.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <sys/ioctl.h>
  34. #include <net/if.h>
  35. #include <stdio.h>
  36.  
  37.  
  38. static int s;  /* socket for route IOCTLs */
  39.  
  40. /*
  41.  *    Get configuration of directly connected interfaces
  42.  */
  43.  
  44. void kr_getifconf(void)
  45. {
  46.    struct ifconf ifc;
  47.    struct ifreq *ife,ifr;
  48.    struct tb_iface *iface;
  49.    int ifi;
  50.    short ifflags;
  51.  
  52.    if((s=socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  53.       warn1(ERCKR_NOIPSOCK,errno);
  54.       goto getif_failed;
  55.    }
  56.    ifc.ifc_len=sizeof(page);
  57.    ifc.ifc_buf=page;
  58.  
  59.       /* get configuration of the interfaces */
  60.    if(ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
  61.       warn2(ERCKR_IOCTL,SIOCGIFCONF,errno);
  62.       goto getif_failed;
  63.    }
  64.  
  65.       /* step through interfaces */
  66.    for(ife=ifc.ifc_req; (char *) ife < (char *) ifc.ifc_req
  67.                                        +ifc.ifc_len; ife++) {
  68.  
  69.       if(!ife->ifr_name[0])
  70.      continue;
  71.  
  72.       ifi=tb_newiface();
  73.       if(ifi==-1) {
  74.      die0(ERCKR_IFFULL);
  75.       }
  76.  
  77.       iface=&tb_iface[ifi];
  78.       memcpy(iface->tbif_dev,ife->ifr_name,IFNAMSIZ);
  79.       iface->tbif_myaddr=ife->ifr_addr;
  80.  
  81.       memcpy(ifr.ifr_name,iface->tbif_dev,IFNAMSIZ);
  82.  
  83. #define IFIOCTL(s,num,arg) \
  84.       if(ioctl((s),(num),(arg)) < 0) {        \
  85.          warn2(ERCKR_IOCTL,(num),errno);        \
  86.          continue;                \
  87.       }
  88.  
  89.       IFIOCTL(s, SIOCGIFFLAGS, (char *)&ifr);
  90.       ifflags=ifr.ifr_flags;
  91.       if((ifflags&IFF_UP)==0) {
  92.      continue;
  93.       }
  94.       if(ifflags&IFF_LOOPBACK) {
  95.      iface->tbif_flags|=TBIFF_LOOPBACK;
  96.       }
  97.       if(ifflags&IFF_POINTOPOINT) {
  98.      iface->tbif_flags|=TBIFF_POINTOPOINT;
  99.      IFIOCTL(s, SIOCGIFDSTADDR, (char *)&ifr);
  100.      iface->tbif_dstaddr=ifr.ifr_dstaddr;
  101.       }
  102.       if(ifflags&IFF_BROADCAST) {
  103.      iface->tbif_flags|=TBIFF_BROADCAST;
  104.      IFIOCTL(s, SIOCGIFBRDADDR, (char *)&ifr);
  105.      iface->tbif_broadaddr=ifr.ifr_broadaddr;
  106.       }
  107.       IFIOCTL(s, SIOCGIFNETMASK, (char *)&ifr);
  108.       iface->tbif_netmask=ifr.ifr_netmask;
  109.       IFIOCTL(s, SIOCGIFMETRIC, (char *)&ifr);
  110.       iface->tbif_metric=ifr.ifr_metric+1;
  111.       if(iface->tbif_metric<1 || iface->tbif_metric >= TBM_INFINITY)
  112.      iface->tbif_metric=1;  /* bad metric, so make one up */
  113.       iface->tbif_flags|=TBIFF_USED|TBIFF_UP;
  114.    }
  115.  
  116.  getif_failed:
  117.    ;
  118.  
  119. }
  120.  
  121.  
  122. /*
  123.  *    Delete a route from the kernel tables
  124.  */
  125.  
  126. void kr_delroute( int route )
  127. {
  128.    struct rtentry kr;
  129.  
  130.    kr.rt_dst = tb_route[route].tbrt_dst;
  131.    kr.rt_dev = NULL;
  132.  
  133.    if( ioctl( s, SIOCDELRT, (char *) &kr ) < 0 ) {
  134.       warn1(ERCKR_XDELRT, errno);
  135.    }
  136.    note1( ERCKR_DELRT, route );
  137. }
  138.  
  139.  
  140. /*
  141.  *    Add a route to the kernel tables
  142.  */
  143.  
  144. void kr_addroute( int route )
  145. {
  146.    struct rtentry kr;
  147.    struct tb_route *rtp;
  148.  
  149.    rtp = &tb_route[route];
  150.    kr.rt_dst = rtp->tbrt_dst;
  151.    kr.rt_gateway = rtp->tbrt_gateway;
  152.    kr.rt_genmask = rtp->tbrt_mask;
  153.    kr.rt_metric = rtp->tbrt_metric;
  154.    kr.rt_flags = RTF_UP;
  155.    if( rtp->tbrt_flags & TBRTF_DIRECT ) {
  156.       if( rtp->tbrt_iface != -1 )
  157.      kr.rt_dev = tb_iface[rtp->tbrt_iface].tbif_dev;
  158.    } else {
  159.       kr.rt_dev = NULL;
  160.       kr.rt_flags |= RTF_GATEWAY;
  161.    }
  162.    if( rtp->tbrt_flags & TBRTF_HOST )
  163.       kr.rt_flags |= RTF_HOST;
  164.  
  165.    if( ioctl( s, SIOCADDRT, (char *) &kr ) < 0 ) {
  166.       warn1(ERCKR_XADDRT, errno);
  167.    }
  168.    note1( ERCKR_ADDRT, route );
  169. }
  170.